<?php

namespace app\admin\model\sum\video;

use app\admin\model\sum\Category;
use app\admin\model\sum\Common;
use app\admin\model\sum\Config as SumConfig;
use traits\model\SoftDelete;

class Video extends Common
{
    use SoftDelete;

    protected $deleteTime = 'deletetime';
    
    // 表名
    protected $name = 'sum_video';
    

    // 追加属性
    protected $append = [
        'lang_text',
        'status_text',
        'image_url'
    ];

    // 语言列表
    public function langList()
    {
        $configs = SumConfig::getConfigs('language.option', false);
        $data=[];
        foreach ($configs as $cv){
            $data[$cv['slug']]=$cv['title'];
        }
        return $data;
    }
    /**
     * 获取器
     * @param string $value
     * @param array $data
     * @return string
     */
    public function getLangTextAttr($value, $data)
    {
        $value = $value ?: ($data['lang'] ?? null);

        $list = $this->langList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    /**
     * 修改器 category_ids
     *
     * @param array|string $value
     * @param array $data
     * @return string
     */
    public function setCategoryIdsAttr($value, $data)
    {
        $category_ids = is_array($value) ? join(',', $value) : $value;
        return $category_ids;
    }
    /**
     * 获取 category_ids数组
     *
     * @param [type] $value
     * @param [type] $data
     * @return void
     */
    public function getCategoryIdsAttr($value, $data)
    {
        $category_ids = $this->attrFormatComma($value, $data, 'category_ids', true);

        return $category_ids ? array_values(array_filter(array_map("intval", $category_ids))) : $category_ids;
    }
    /**
     * 获取所有分类列表
     *
     * @param string $value
     * @param array $data
     * @return array
     */
    public function getCategoryAttr($value, $data)
    {
        $category_ids = $this->attrFormatComma($value, $data, 'category_ids');

        $categoryData = [];
        if ($category_ids) {
            $categoryData = Category::whereIn('id', $category_ids)->select();
        }
        return $categoryData;
    }

    public function getImageUrlAttr($value, $data)
    {
        $image_url = $data['image']?cdnurl($data['image'], true):'';
        return $image_url;
    }
}
